from django.db import models
from user.models import UserModel, AddressModel
from prod.models import GoodsModel


# Create your models here.

class OrderModel(models.Model):
    """订单表"""
    status_text = (
        (0, "待支付"),
        (1, "待发货"),
        (2, "待收货"),
        (3, "已完成"),
        (4, "售后中"),
    )
    pay_text = (
        (0, "支付宝"),
        (1, "微信"),
        (2, "银联"),
    )
    o_id = models.CharField(max_length=32, default="", verbose_name="订单号")
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="下单用户")
    status = models.IntegerField(choices=status_text, verbose_name="支付状态")
    pay_type = models.IntegerField(choices=pay_text, verbose_name="支付方式")
    addr = models.ForeignKey(AddressModel, on_delete=models.CASCADE, verbose_name="收货地址")
    total_count = models.IntegerField(verbose_name="商品总数")
    total_amount = models.DecimalField(max_digits=12, decimal_places=2, verbose_name="商品总金额")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="下单时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="上次状态更新时间")

    class Meta:
        db_table = "order"


class OrderGoodsModel(models.Model):
    goods = models.ForeignKey(GoodsModel, on_delete=models.CASCADE, verbose_name="购买的商品")
    count = models.IntegerField(verbose_name="购买数量")
    price = models.DecimalField(max_digits=12, decimal_places=2, verbose_name="购买时的价格")
    order = models.ForeignKey(OrderModel, on_delete=models.CASCADE, verbose_name="所属订单")

    class Meta:
        db_table = "order_goods"
